/*
 * Copyright (c) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <limits.h>

#include "cJSON.h"

#include "hi_ext_util.h"
#include "cjson_help.h"

/**
    获取指定object的指定item的string值.
*/
const char* JsonStr(cJSON* obj, const char* itemName, const char* defVal)
{
    HI_ASSERT(obj);
    HI_ASSERT(itemName && *itemName);
    cJSON *item;

    item = cJSON_GetObjectItem(obj, itemName);
    if (!item) {
        return defVal ? defVal : "";
    }

    if (item->type == cJSON_String) {
        HI_ASSERT(item->valuestring);
        return item->valuestring;
    } else {
        return defVal ? defVal : "";
    }
}

/**
    获取指定object的指定item的int值.
*/
int JsonInt(cJSON* obj, const char* itemName, int defVal)
{
    HI_ASSERT(obj);
    HI_ASSERT(itemName && *itemName);
    cJSON *item;

    item = cJSON_GetObjectItem(obj, itemName);
    if (!item) {
        return defVal;
    }

    if (item->type == cJSON_Number) {
        return item->valueint;
    } else if (item->type == cJSON_String) {
        HI_ASSERT(item->valuestring);
        return atoi(item->valuestring);
    } else {
        return defVal;
    }
}

/**
    获取指定object的指定item的double值.
*/
double JsonDouble(cJSON* obj, const char* itemName, double defVal)
{
    HI_ASSERT(obj);
    HI_ASSERT(itemName && *itemName);
    cJSON *item;

    item = cJSON_GetObjectItem(obj, itemName);
    if (!item) {
        return defVal;
    }

    if (item->type == cJSON_Number) {
        return item->valueint;
    } else if (item->type == cJSON_String) {
        HI_ASSERT(item->valuestring);
        return strtod(item->valuestring, NULL);
    } else {
        return defVal;
    }
}

/**
    获取指定object的指定item的bool值.
*/
bool JsonBool(cJSON* obj, const char* itemName, bool defVal)
{
    HI_ASSERT(obj);
    HI_ASSERT(itemName && *itemName);
    cJSON *item;

    item = cJSON_GetObjectItem(obj, itemName);
    if (!item) {
        return defVal;
    }

    if (item->type == cJSON_True) {
        return true;
    } else if (item->type == cJSON_False) {
        return false;
    } else if (item->type == cJSON_NULL) {
        return false;
    } else if (item->type == cJSON_Number) {
        return item->valueint ? true : false;
    } else if (item->type == cJSON_String) {
        HI_ASSERT(item->valuestring);
        return atoi(item->valuestring) ? true : false;
    } else {
        return defVal;
    }
}

